package com.pwx.web.controller;

import com.github.pagehelper.PageInfo;
import com.pwx.common.Const;
import com.pwx.entity.Emp;
import com.pwx.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: EmpController
 * Author: saber
 * Date:2022/9/29 18:47
 */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpServices es;


    @RequestMapping("/getAll")
    public String getAll(@RequestParam Map<String,Object> maps, Model model){
        PageInfo<Emp> info = es.getAll(maps);
        model.addAttribute("info",info);
        return "list";
    }

    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String doLogin(String userName, String password, HttpSession session){
        Emp emp = es.doLoging(userName,password);
        if (emp!=null) {
            session.setAttribute(Const.IS_LOGIN,emp);
            return "redirect:/emp/getAll";
        }else {
            return "redirect:/login.jsp";
        }
    }

    @RequestMapping(value = "/addEmp",method = RequestMethod.POST)
    @ResponseBody
    public String addEmp(@RequestBody Emp emp){
        System.out.println(111111111);
        System.out.println(emp);
        boolean f = es.addToEmp(emp);
        if (f){
            return "1";
        }else {
            return "0";
        }
    }

} 
